package com.zmn.plat.business.impl.extension.serv.category.group;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.plat.business.interfaces.extension.serv.category.ExtensionServCategoryGroupBService;
import com.zmn.plat.common.exception.PlatException;
import com.zmn.plat.model.entity.extension.serv.category.ExtensionServCategoryAssociation;
import com.zmn.plat.model.entity.extension.serv.category.group.ExtensionServCategoryGroup;
import com.zmn.plat.services.interfaces.extension.serv.category.ExtensionServCategoryAssociationService;
import com.zmn.plat.services.interfaces.extension.serv.category.group.ExtensionServCategoryGroupService;

/**
 * 类描述:服务分类关联推广分类BService
 *
 * @author linfeng
 * @since 2019/12/24/17:59
 */
@Service
public class ExtensionServCategoryGroupBServiceImpl implements ExtensionServCategoryGroupBService {
    private Logger logger = LoggerFactory.getLogger(ExtensionServCategoryGroupBServiceImpl.class);

    @Resource
    private ExtensionServCategoryGroupService extensionServCategoryGroupService;

    @Resource
    private ExtensionServCategoryAssociationService extensionServCategoryAssociationService;

    /**
     * 批量插入推广服务分类关联的推广分类，插入前先删除原来的关联
     * 
     * @param creater
     *            操作人
     * @param object
     *            分类关联关系
     * @author linfeng
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer batchAddAssociation(String creater, JSONObject object) {
        if (null == object) {
            throw new PlatException("参数不能为空！");
        }

        String servCategoryId = object.getString("servCategoryId");
        JSONArray extensionServCategoryGroupList = object.getJSONArray("extensionServCategoryGroupList");
        List<ExtensionServCategoryGroup> list = new ArrayList<>();

        for (int i = 0; i < extensionServCategoryGroupList.size(); i++) {
            ExtensionServCategoryGroup extensionServCategoryGroup = new ExtensionServCategoryGroup();
            ExtensionServCategoryGroup result = JSONObject.parseObject(extensionServCategoryGroupList.getString(i), ExtensionServCategoryGroup.class);
            extensionServCategoryGroup.setExtensionServCategId(result.getExtensionServCategId());
            extensionServCategoryGroup.setCategId(result.getCategId());
            extensionServCategoryGroup.setCreater(creater);
            extensionServCategoryGroup.setCreateTime(DateUtil.getNow());
            list.add(extensionServCategoryGroup);
        }

        Integer result = 0;
        try {
            result = extensionServCategoryGroupService.deleteByServCategId(Integer.parseInt(servCategoryId));
            result = extensionServCategoryGroupService.insertBatch(list);
        } catch (org.springframework.dao.DuplicateKeyException ex) {
            logger.error("关联已经存在，不能重复添加！");
            throw new PlatException("关联已经存在，不能重复添加！");
        } catch (Exception e) {
            throw new PlatException("操作失败");
        }

        return result;
    }

    @Override
    public Integer batchRelateBackCategory(String creater, JSONObject object) {
        String extensionServCategId = object.getString("extensionServCategId");
        String extensionCategId = object.getString("extensionCategId");
        JSONArray extensionServCategoryAssociationList = object.getJSONArray("extensionServCategoryAssociationList");
        List<ExtensionServCategoryAssociation> list = new ArrayList<>();

        for (int i = 0; i < extensionServCategoryAssociationList.size(); i++) {
            ExtensionServCategoryAssociation extensionServCategoryAssociation = new ExtensionServCategoryAssociation();
            ExtensionServCategoryAssociation result = JSONObject.parseObject(extensionServCategoryAssociationList.getString(i), ExtensionServCategoryAssociation.class);
            extensionServCategoryAssociation.setExtensionServCategId(result.getExtensionServCategId());
            extensionServCategoryAssociation.setExtensionCategId(result.getExtensionCategId());
            extensionServCategoryAssociation.setCategId(result.getCategId());
            extensionServCategoryAssociation.setCreater(creater);
            extensionServCategoryAssociation.setCreateTime(DateUtil.getNow());
            list.add(extensionServCategoryAssociation);
        }

        Integer result = 0;
        try {
            result = extensionServCategoryAssociationService.deleteByExtensionServCategId(Integer.parseInt(extensionServCategId), Integer.parseInt(extensionCategId));
            result = extensionServCategoryAssociationService.insertBatch(list);
        } catch (org.springframework.dao.DuplicateKeyException ex) {
            logger.error("关联已经存在，不能重复添加！");
            throw new PlatException("关联已经存在，不能重复添加！");
        } catch (Exception e) {

            throw new PlatException("操作失败");
        }

        return result;
    }
}
